package sgg.algorithm.datastructure.sort;

import java.util.Arrays;

/**
 * @author: Michael
 * @date: 2025/4/30
 * @desc: 插入排序
 */
public class InsertionSort {

    public static void main(String[] args) {
        int[] arr = {101, 34, 119, 1};
        System.out.println(Arrays.toString(insertionSort(arr)));
    }

    public static int[] insertionSort(int[] arr) {
        int[] result = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            result[i] = arr[i];
            for (int j = i - 1; j >= 0; j--) {
                if (result[j] > result[j + 1]) {
                    int temp = result[j];
                    result[j] = result[j + 1];
                    result[j + 1] = temp;
                }
            }
        }
        return result;
    }
}
